package com.cloudlinkscm.base.authcenter.controller;

import com.cloudlinkscm.base.authcenter.assembler.UserAccount2UserAccountInfoDtoAssembler;
import com.cloudlinkscm.base.authcenter.domain.def.DefaultUserAccountManager;
import com.cloudlinkscm.base.authcenter.model.dto.UserAccountActiveDto;
import com.cloudlinkscm.base.authcenter.model.dto.UserAccountCreateDto;
import com.cloudlinkscm.base.authcenter.model.dto.UserAccountPasswordChangeDto;
import com.cloudlinkscm.base.authcenter.model.dto.UserAccountSearchDTO;
import com.cloudlinkscm.base.authcenter.model.vo.UserAccountVO;
import com.cloudlinkscm.base.authcenter.model.vo.UserRoleRelationDistributionVO;
import com.cloudlinkscm.base.authcenter.service.UserAccountRemoteService;
import com.cloudlinkscm.loms.framework.core.constant.SwaggerExample;
import com.cloudlinkscm.loms.framework.core.pojo.RestfulApiResponse;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : tac
 * @date : 31/07/2017
 */

@RestController
@RequestMapping(UserAccountRemoteService.PATH)
public class UserAccountController implements UserAccountRemoteService {

    @Autowired
    private DefaultUserAccountManager userAccountManager;
    @Autowired
    private UserAccount2UserAccountInfoDtoAssembler userAccount2UserAccountInfoDtoAssembler;

    @ApiOperation("新建用户")
    @RequestMapping(value = "create", method = RequestMethod.PUT)
    @Override
    public RestfulApiResponse create(@RequestBody UserAccountCreateDto dto){
        return RestfulApiResponse.success("用户创建成功", userAccount2UserAccountInfoDtoAssembler.assemble(userAccountManager.create(dto)));
//        return RestfulApiResponse.success("用户创建成功", userAccountManager.create(dto));
    }

	@ApiOperation("删除指定用户")
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public RestfulApiResponse<Integer> delete(
            @ApiParam(value = "用户id", required = true, defaultValue = SwaggerExample.UUID) @RequestParam String userId){
        return RestfulApiResponse.success("用户删除成功", userAccountManager.delete(userId));
    }

    @ApiOperation("修改用户密码")
    @RequestMapping(value = "changePassword", method = RequestMethod.PUT)
    public RestfulApiResponse<Integer> changePassword(@RequestBody UserAccountPasswordChangeDto dto){
        return RestfulApiResponse.success("修改密码成功", userAccountManager.changePassword(dto));
    }

    @ApiOperation("激活用户账号")
    @RequestMapping(value = "active", method = RequestMethod.PUT)
    public RestfulApiResponse<Integer> active(@RequestBody UserAccountActiveDto dto){
        return RestfulApiResponse.success("用户激活成功", userAccountManager.active(dto));
    }

    @ApiOperation("启用用户账号")
    @RequestMapping(value = "enable", method = RequestMethod.PUT)
    public RestfulApiResponse<Integer> enable(
            @ApiParam(value = "用户id", required = true, defaultValue = SwaggerExample.UUID) @RequestParam String userId){
        return RestfulApiResponse.success("用户激活成功", userAccountManager.enable(userId));
    }

    @ApiOperation("禁用用户账号")
    @RequestMapping(value = "disable", method = RequestMethod.PUT)
    public RestfulApiResponse<Integer> disable(
            @ApiParam(value = "用户id", required = true, defaultValue = SwaggerExample.UUID) @RequestParam String userId){
        return RestfulApiResponse.success("用户激活成功", userAccountManager.disable(userId));
    }


    ///
	/// role relation

	@ApiOperation("账户列表")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public RestfulApiResponse<UserAccountVO> list(@RequestBody UserAccountSearchDTO searchDTO){
		return RestfulApiResponse.success("操作成功", userAccountManager.list(searchDTO));
	}

	@ApiOperation("用户与权限：权限列表")
	@RequestMapping(value = "{userId}/role", method = RequestMethod.GET)
	public RestfulApiResponse<UserRoleRelationDistributionVO> role(@PathVariable("userId") String userId){
		return RestfulApiResponse.success(userAccountManager.role(userId));
	}

	@ApiOperation("用户与权限：保存")
	@RequestMapping(value = "{userId}/role", method = RequestMethod.POST)
	public RestfulApiResponse roleSave(@PathVariable("userId") String userId,
	                                                                   @RequestBody List<String> roles){
		return RestfulApiResponse.success("操作成功", userAccountManager.roleSave(userId, roles));
	}

}
